﻿using System;
using System.Data;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
using System.Security;
using System.Text;
using System.Configuration;

namespace StockTrader.DAL.HoseAdapter.Lib
{
	/// <summary>
	/// Summary description for CommonLib.
	/// </summary>
	public class CommonLib
	{
		public CommonLib()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		#region Conversation functions
		public static bool CheckNullBool(object Value)
		{
			if ((Value==DBNull.Value)||(Value==null)||(Value.ToString().Trim()=="")) return false;
			else return Convert.ToBoolean(Value);
		}

		public static byte CheckNullByte(object Value)
		{
			if (Value==DBNull.Value) return 0;
			else return Convert.ToByte(Value);
		}

		public static int CheckNullInt(object Value)
		{
			if (Value==DBNull.Value) return 0;
			else return Convert.ToInt32(Value);
		}

		public static int CheckNullInt(string Value)
		{
			if (Value=="") return 0;
			else return Convert.ToInt32(Value);
		}

		public static double CheckNullDbl(object Value)
		{
			if (Value==DBNull.Value) return 0;
			else return Convert.ToDouble(Value);
		}

		public static float CheckNullFloat(object Value)
		{
			if (Value==DBNull.Value) return 0;
			else return Convert.ToSingle(Value);
		}

		public static long CheckNullLong(object Value)
		{
			if (Value==DBNull.Value) return 0;
			else return Convert.ToInt64(Value);
		}

		public static string CheckNullStr(object Value)
		{
			if (Value==DBNull.Value) return "";
			else return Value.ToString();
		}

		public static DateTime CheckNullDate(object Value)
		{
			if (Value==DBNull.Value) return DateTime.MinValue;
			else return Convert.ToDateTime(Value);
		}

		public static object CheckDBNullDate(object Value)
		{
			if (Convert.ToDateTime(Value)==DateTime.MinValue) return DBNull.Value;
			else return Convert.ToDateTime(Value);
		}

		public static string ShowNullDate(DateTime Value)
		{
			if (Value==DateTime.MinValue) return "";
			else return Value.ToString("dd/MM/yyyy");
		}

		public static DateTime GetNullDate(string Value)
		{
			if (Value.Trim()=="") return DateTime.MinValue;
			else return ToDate(Value,"dd/MM/yyyy");
		}

		public static DateTime ToDate(string x,string kieu)
		{
			try
			{
				int sp1 = x.IndexOf('/'), sp2 = x.LastIndexOf('/');
				int day,month,year;				
				if (kieu.Equals("MM/dd/yyyy")) // MM/dd/yyyy hoac M/d/yyyy
				{
					day = int.Parse(x.Substring(sp1+1,sp2-sp1-1));
					month = int.Parse(x.Substring(0,sp1));
				}
				else //'dd/MM/yyyy' hoac d/M/yyyy
				{
					day = int.Parse(x.Substring(0,sp1));
					month = int.Parse(x.Substring(sp1+1,sp2-sp1-1));
				}
				year = int.Parse(x.Substring(sp2+1,4));
				return new DateTime(year,month,day);
			}
			catch 
			{
				throw new Exception("Sai kieu ngay thang");
			}
		}

		public static bool IsInt(string Value)
		{
			try
			{
				Convert.ToInt32(Value);
				return true;
			}
			catch
			{
				return false;
			}
		}
		#endregion

        public static string PreventSQLInjection(string strSQL)
        {
            String badcommands = "INSERT,UPDATE,DELETE,DROP,CREATE,UNION,SP_,XP_";
            String strRet = strSQL.ToUpper();
            String[] arrBadCommand = badcommands.Split(',');
            if (arrBadCommand.GetLength(0) > 0)
            {
                for (int i = 0; i < arrBadCommand.GetLength(0); i++)
                {
                    if (strRet.Contains(arrBadCommand[i]))
                    {
                        strRet = strRet.Replace(arrBadCommand[i], " ");
                    }
                }
            }

            return strRet.Replace("'", "''");
        }

        public static string GetKeyValue(string KeyName)
        {
            try
            {
                return ConfigurationManager.AppSettings[KeyName];
            }
            catch
            {
                return "";
            }
        }

        public static bool IsNumeric(string value)
        {
            try
            {
                int.Parse(value);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static void CopyStream(Stream from, Stream to)
        {
            byte[] buffer = new byte[1024];
            int intByteCount = -1;
            from.Seek(0, SeekOrigin.Begin);
            while ((intByteCount = from.Read(buffer, 0, 1024)) > 0)
                to.Write(buffer, 0, intByteCount);

            to.Seek(0, SeekOrigin.Begin);
        }

        public static void SafeCopy(string source, string des)
        {
            //fi = null, fo = null;
            try
            {
                int num = 0;
                byte[] b = new byte[32000];
                using (FileStream fi = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    using (FileStream fo = new FileStream(des, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
                    {
                        do
                        {
                            num = fi.Read(b, 0, 32000);
                            if (num > 0) fo.Write(b, 0, num);
                        }
                        while (num > 0);
                        fi.Close();
                        fo.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
	}
}
